﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="ContainerConfigurationManager.cs" company="ELEN 7045 GROUP II">
//   Contributed by E.V.
// </copyright>
// <summary>
//   A ContainerConfigurationManager Implementation
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace Infrastructure.CrossCutting.IoC
{
    using System;
    using System.Configuration;

    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;

    /// <summary>
    /// A ContainerConfigurationManager Implementation
    /// </summary>
    public sealed class ContainerConfigurationManager : IContainerConfigurationManager
    {
        /// <summary>
        /// Gets the container configuration.
        /// </summary>
        /// <param name="configurationFileName">Name of the configuration file.</param>
        /// <param name="containerConfigurationSectionName">Name of the container configuration section.</param>
        /// <returns>A configured container</returns>
        IUnityContainer IContainerConfigurationManager.GetContainerConfiguration(string configurationFileName, string containerConfigurationSectionName)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Gets the container.
        /// </summary>
        /// <param name="containerFileNameKey">The container file name key.</param>
        /// <returns>A configured container</returns>
        public IUnityContainer GetContainer(string containerFileNameKey)
        {
            var filename = ConfigurationManager.AppSettings.Get(containerFileNameKey);

            if (string.IsNullOrEmpty(filename))
            {
                throw new ArgumentException(
                    "The container filename provided in the configuration file is invalid. Ensure that it is not empty");
            }

            return LoadConfiguration(filename);
        }

        /// <summary>
        /// Loads the unity configuration from the specified file.
        /// </summary>
        /// <param name="fileName">Name of the file.</param>
        /// <returns>A configured unity container</returns>
        private static IUnityContainer LoadConfiguration(string fileName)
        {
            var path = AppDomain.CurrentDomain.BaseDirectory + "\\" + fileName;

            if (!System.IO.File.Exists(path))
            {
                throw new ArgumentException(
                    "The following unity container configuration file cannot be found: {0}", path);
            }

            var map = new ExeConfigurationFileMap
            {
                ExeConfigFilename = path
            };

            var config = ConfigurationManager.OpenMappedExeConfiguration(map, ConfigurationUserLevel.None);
            var section = (UnityConfigurationSection)config.GetSection("unity");
            /*I know its not nice restricting the section name... could use a refactor*/

            IUnityContainer container = new UnityContainer();
            container.LoadConfiguration(section);

            return container;
        }
    }
}
